<?php
namespace Admin\Controller;
use Think\Controller;
class LoginController extends Controller {
	//登录。
	public function index(){		
		if(IS_POST){
			$p=I('post.');
			if(!$this->check_verify($p['yzm'])){
				$this->error('验证码错误！');
			}
			$admin=M('admin_user')->where(array('user'=>$p['user']))->find();
			if($admin['pass']!=md5($p['pass'])){
				$this->error('您检查一下您的账户和密码有没有输错！');
			}

			$img=$this->getPic($admin['img']);
			$admin['img']=$img['imgurl'];

			if($admin){
				$admin['rolename']=M('admin_role')->where(array('id'=>$admin['role']))->getField('name');
				$_SESSION['admin']['is_login']=1;
				$_SESSION['admin']['user']=$admin;
			}
			if($_SESSION['admin']['is_login']){
				$u['id']=$admin['id'];
				$u['logintime']=time();
				$u['loginip']=get_client_ip();
				M('admin_user')->save($u);
				
				
				$this->redirect(U('/admin/Index/index'));
			}
		}
		$this->display();
	}
	
	//退出
	public function out(){
		session_unset();
		$this->redirect(U('/admin/Index/index'));
	}
	
	//验证码。
	public function yzm(){
		$config =    array(
		    'fontSize'    =>    30,    // 验证码字体大小
		    'length'      =>    4,     // 验证码位数
		    'useCurve'	  =>		FALSE,
		    'useNoise'    =>    true, // 关闭验证码杂点
		    'codeSet'	  =>	'0123456789',
		    'imageH'	  =>	'66',
		);
		$Verify = new \Think\Verify($config);
		$Verify->entry();
	}
	// 检测输入的验证码是否正确，$code为用户输入的验证码字符串
	function check_verify($code){
	    $verify = new \Think\Verify();
	    return $verify->check($code);
	}
	
	//获取单张图片地址
	public function getPic($id){
		$m=M('weixin_upload_img');
		$map['id']=$id;
		$list=$m->where($map)->find();
		if($list){
			$list['imgurl']=C('BB_URL')."/Upload/".$list['savepath'].$list['savename'];
		}
		return $list?$list:"";
	}

	//百度地图
	public function baiduDitu(){
		$map['address']=I('address');
		$map['lng']=I('lng');
		$map['lat']=I('lat');
		$this->assign('map',$map);
		$mb=$this->fetch();
		$this->ajaxReturn($mb);
	}
	
}